<?php

if (!defined('DEFAULT_LOCALE')) {
    define('DEFAULT_LOCALE', 'ru');
}

class Adept_L10n_Bundle {
	
	const DEFAULT_GROUP = 'default';
	const USE_DEFAULT_LOCALE = null;
	
	static protected $instance = null;
	
	protected $strings;
	
	public function __construct() {
	}
	
	static function getInstance() {
		if (self::$instance == null) {
			self::$instance = new self();
		}
		return self::$instance;			
	}
	
	protected function loadBundleFile($group, $locale)
	{
	    $locator = new Adept_Locator_Bundle();
	    if ($file = $locator->locate($group, array(Adept_Locator_Bundle::LOCALE_PARAM  => $locale))) {
    	    $ini = new Adept_Config_Ini($file);
    	    $this->strings[$group] = $ini->getValuesList();
    	    return true;
	    } else {
	        return false;
	    }
	}
	
	static function get($key, $group = self::DEFAULT_GROUP, $locale = self::USE_DEFAULT_LOCALE)
	{
		$instance = self::getInstance();
		
		if ($locale == self::USE_DEFAULT_LOCALE) {
			$locale = DEFAULT_LOCALE; // REFACTOR
		}
		if (!isset($instance->strings[$group]))	{
			if (!$instance->loadBundleFile($group, $locale)) {
				return $key;
			}
		}
		
		return isset($instance->strings[$group][$key]) ? $instance->strings[$group][$key] : $key;
	}
	
}
